package com.gulimall.product.app;

import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;
import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.entity.AttrGroupEntity;
import com.gulimall.product.service.AttrAttrgroupRelationService;
import com.gulimall.product.service.AttrGroupService;
import com.gulimall.product.vo.AttrGroupWithAttrVo;
import com.gulimall.product.vo.AttrRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 属性分组
 *
 * @author kkk
 * @email 2017809755@qq.com
 * @date 2022-05-17 21:49:10
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    ///api/product/attrgroup/1/attr/relation
    @GetMapping("/{catelogId}/attr/relation")
    public R attrRelation(@PathVariable Long catelogId) {
        List<AttrEntity> list = attrGroupService.getAttrRelation(catelogId);

        return R.ok().put("data", list);
    }

    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrRelationVo> relationVos) {
        relationService.saveBatchRelation(relationVos);

        return R.ok();
    }

    /**
     * 根据分组查出所有的属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupByCatelogId(@PathVariable Long catelogId) {
        //查询当前分类下面所有属性的分组
        List<AttrGroupEntity> attrGroupEntities = attrGroupService.queryAttrGroupByCatelogId(catelogId);

        //查询每个属性下面的所有分组
        List<AttrGroupWithAttrVo> groupWithAttrVos = attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttrVo groupWithAttrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(item, groupWithAttrVo);
            groupWithAttrVo.setAttrs(attrGroupService.getAttrRelation(item.getAttrGroupId()));
            return groupWithAttrVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", groupWithAttrVos);
    }

    /**
     * 批量删除
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrRelationVo> relationVo) {
        attrGroupService.matchDeleteRelation(relationVo);

        return R.ok();
    }

    /**
     * 获取属性分组没有关联的其他属性
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R noAttrRelation(@PathVariable Long attrGroupId, @RequestParam Map<String, Object> params) {
        PageUtils pageUtils = attrGroupService.getNoattrRelation(params, attrGroupId);

        return R.ok().put("page", pageUtils);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{category}")

    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long category) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, category);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
//        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        AttrGroupEntity attrGroup = attrGroupService.getGroupPaths(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
